package com.jiumi.baseconfig.service.impl;

import java.util.List;
import java.util.Optional;

import com.jiumi.baseconfig.domain.BaseCourseType;
import com.jiumi.baseconfig.domain.BaseTeacher;
import com.jiumi.baseconfig.mapper.BaseCourseTypeMapper;
import com.jiumi.baseconfig.mapper.BaseTeacherMapper;
import com.jiumi.common.utils.DateUtils;
import com.jiumi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiumi.baseconfig.mapper.BaseCourseInfoMapper;
import com.jiumi.baseconfig.domain.BaseCourseInfo;
import com.jiumi.baseconfig.service.IBaseCourseInfoService;

/**
 * 课程信息Service业务层处理
 *
 * @author jiumi
 * @date 2023-05-21
 */
@Service
public class BaseCourseInfoServiceImpl implements IBaseCourseInfoService {
    @Autowired
    private BaseCourseInfoMapper baseCourseInfoMapper;
    @Autowired
    private BaseTeacherMapper baseTeacherMapper;

    @Autowired
    private BaseCourseTypeMapper courseTypeMapper;

    /**
     * 查询课程信息
     *
     * @param id 课程信息主键
     * @return 课程信息
     */
    @Override
    public BaseCourseInfo selectBaseCourseInfoById(Long id) {
        return baseCourseInfoMapper.selectBaseCourseInfoById(id);
    }

    /**
     * 查询课程信息列表
     *
     * @param baseCourseInfo 课程信息
     * @return 课程信息
     */
    @Override
    public List<BaseCourseInfo> selectBaseCourseInfoList(BaseCourseInfo baseCourseInfo) {
        return baseCourseInfoMapper.selectBaseCourseInfoList(baseCourseInfo);
    }

    /**
     * 新增课程信息
     *
     * @param baseCourseInfo 课程信息
     * @return 结果
     */
    @Override
    public int insertBaseCourseInfo(BaseCourseInfo baseCourseInfo) {
        baseCourseInfo.setCreateTime(DateUtils.getNowDate());
        Long teacherId = baseCourseInfo.getTeacherId();
        if (StringUtils.isNotNull(teacherId)) {
            BaseTeacher baseTeacher = baseTeacherMapper.selectBaseTeacherById(teacherId);
            if (StringUtils.isNotNull(baseTeacher)) {
                baseCourseInfo.setTeacherName(baseTeacher.getName());
            }
        }
        updateCourseType(baseCourseInfo);
        return baseCourseInfoMapper.insertBaseCourseInfo(baseCourseInfo);
    }

    /**
     * 修改课程信息
     *
     * @param baseCourseInfo 课程信息
     * @return 结果
     */
    @Override
    public int updateBaseCourseInfo(BaseCourseInfo baseCourseInfo) {
        baseCourseInfo.setUpdateTime(DateUtils.getNowDate());
        Long teacherId = baseCourseInfo.getTeacherId();
        if (StringUtils.isNotNull(teacherId)) {
            BaseTeacher baseTeacher = baseTeacherMapper.selectBaseTeacherById(teacherId);
            if (StringUtils.isNotNull(baseTeacher)) {
                baseCourseInfo.setTeacherName(baseTeacher.getName());
            }
        }
        updateCourseType(baseCourseInfo);
        return baseCourseInfoMapper.updateBaseCourseInfo(baseCourseInfo);
    }

    private void updateCourseType(BaseCourseInfo baseCourseInfo) {
        Long courseTypeId = baseCourseInfo.getCourseTypeId();
        if(StringUtils.isNotNull(courseTypeId)){
            BaseCourseType baseCourseType = courseTypeMapper.selectBaseCourseTypeById(courseTypeId);
            baseCourseInfo.setCourseType(baseCourseType.getCourseType());

        }
    }

    /**
     * 批量删除课程信息
     *
     * @param ids 需要删除的课程信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseCourseInfoByIds(Long[] ids) {
        return baseCourseInfoMapper.deleteBaseCourseInfoByIds(ids);
    }

    /**
     * 删除课程信息信息
     *
     * @param id 课程信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseCourseInfoById(Long id) {
        return baseCourseInfoMapper.deleteBaseCourseInfoById(id);
    }

    @Override
    public List<BaseCourseInfo> selectNotExistsCourseList(BaseCourseInfo baseCourseInfo) {
        return baseCourseInfoMapper.selectNotExistsCourseList(baseCourseInfo);
    }

    @Override
    public List<BaseCourseInfo> selectCourseInfoHomeData() {
        return baseCourseInfoMapper.selectCourseInfoHomeData();
    }

    @Override
    public List<BaseCourseInfo> selectRelateCourseInfoList(BaseCourseInfo courseParam) {
        return baseCourseInfoMapper.selectRelateCourseInfoList(courseParam);
    }

    @Override
    public List<BaseCourseInfo> selectUserBuyCourseInfoList(Long userId) {
        return baseCourseInfoMapper.selectUserBuyCourseInfoList(userId);
    }

    @Override
    public List<BaseCourseInfo> selectUserBuyCourseList(BaseCourseInfo courseInfo) {
        return baseCourseInfoMapper.selectUserBuyCourseList(courseInfo);
    }
}
